package ESP.Util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.sun.corba.se.impl.orbutil.graph.Node;

public class GetXMLConfig {
	/**
	 * 连接相应的数据源
	 */
	private static String DBType = "mysql";
	/**
	 * 单例数据库连接
	 */
	private static Connection connection;
	/**
	 * ——获取XML文件
	 * @param XML的路径
	 * @author 程尧
	 * @return
	 */
	private static Document getXML(String xmlpath){
		//创建DocumentBuilderFactory实例,指定DocumentBuilder
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		Document doc = null;
		try {
			db = dbf.newDocumentBuilder();
			doc = db.parse(xmlpath);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return doc;
	}
	public static String getTable(){
		StringBuffer sb = new StringBuffer("");
		Document doc = getXML("./config-table.xml");
		Element root = doc.getDocumentElement();
		
		NodeList tables = root.getElementsByTagName("table");
		for(int i=0; i<tables.getLength(); i++){
			String tablename = tables.item(i).getAttributes().item(0).getNodeValue().toLowerCase();// tablename
			String tabname = tablename.substring(0, 1).toUpperCase()+tablename.substring(1);
			
			sb.append("【" + tabname + "】\n");
			NodeList fields = root.getElementsByTagName(tablename);
			for (int j = 0; j < fields.getLength(); j++) {
				String item1 = fields.item(j).getAttributes().item(1).getNodeValue();
				String ite0 = fields.item(j).getAttributes().item(0).getNodeValue().toLowerCase();
				sb.append("private ").append(item1).append(" ").append(ite0).append(";//\n");
			}
		}
		return sb.toString();
		
	}
	private static List<String> getDataSource (){
		Document doc = getXML("WebRoot/Config/config-database.xml");//WebRoot/Config/config-database.xml
		List<String> dbsourceList =new ArrayList<String>();//返回集合
		if(doc != null){
			//下面是解析XML的全过程
			Element root = doc.getDocumentElement();//先取根元素"database"
			NodeList databases = root.getElementsByTagName("database");
			boolean flag = false;//循环标志
			for (int i = 0; i < databases.getLength() && !flag; i++) {
				String type = databases.item(i).getAttributes().item(0).getNodeValue();
				if (type.equalsIgnoreCase(DBType)) {//判断DBType
					flag = true;
					//依次取每个"database"元素
					Element database = (Element) databases.item(i);
					NodeList drivers = database.getElementsByTagName("driver");
					if(drivers.getLength() == 1){
						Element driver = (Element) drivers.item(0);
						String text = driver.getFirstChild().getNodeValue();
						dbsourceList.add(text);
					}
					NodeList urls = database.getElementsByTagName("url");
					if(urls.getLength() == 1){
						Element url = (Element) urls.item(0);
						String text = url.getFirstChild().getNodeValue();
						dbsourceList.add(text);
					}
					NodeList usernames = database.getElementsByTagName("username");
					if(usernames.getLength() == 1){
						Element username = (Element) usernames.item(0);
						String text = username.getFirstChild().getNodeValue();
						dbsourceList.add(text);
					}
					NodeList passwords = database.getElementsByTagName("password");
					if(passwords.getLength() == 1){
						Element password = (Element) passwords.item(0);
						String text = password.getFirstChild().getNodeValue();
						dbsourceList.add(text);
					}
				}
			}
		}
		return dbsourceList;
	}
	/**
	 * 在配置文件中可以按照相应格式添加数据源，并且指定DBType。
	 * 这个指定的type的值就是传入的参数，既是打算使用的数据源。
	 * @author ——程尧
	 * @param DBType
	 * @return dbConnection
	 */
	public static Connection getDBConnect(){
		if (connection != null) {
			return connection;
		}
		List<String> dbsourceList =getDataSource();//返回集合
		try {
			Class.forName(dbsourceList.get(0));
			connection = DriverManager.getConnection(dbsourceList.get(1), dbsourceList.get(2), dbsourceList.get(3));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}
}
